﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.Componnent.Common.ToolsHelper;
using WaterRevenueMS.Componnent.Entities.Authen;
using WaterRevenueMS.Core.Service.Authen;
using WaterRevenueMS.Core.Service.SysConfig;
using Ninject;
using WaterRevenueMS.Domain.ViewModels.SysConfig.OperateLog;
using System.ComponentModel.Composition.Hosting;

namespace WaterRevenueMS.WebUI.Extension.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class AdminOperateLogAttribute : ActionFilterAttribute
    {
        public IPermissionService PermissionService { get; set; }
        public IOperateLogService OperateLogService { get; set; }

        public AdminOperateLogAttribute()
        {
            var container = HttpContext.Current.Application["Container"] as CompositionContainer;
            PermissionService = container.GetExportedValueOrDefault<IPermissionService>();
            OperateLogService = container.GetExportedValueOrDefault<IOperateLogService>();
        }


        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            var user = SessionHelper.GetSession("CurrentUser") as User;
            if (user != null)
            {
                var area = filterContext.RouteData.DataTokens["area"] != null ? filterContext.RouteData.DataTokens["area"].ToString() : "";
                var controller = filterContext.RouteData.Values["controller"].ToString();
                var action = filterContext.RouteData.Values["action"].ToString();
                var actionName = action.ToLower();
                var permission = PermissionService.Permissions.FirstOrDefault(t => t.Code.ToLower() == actionName && t.Enabled == true && t.IsDeleted == false);
                var description = string.Empty;
                if (permission != null)
                {
                    description = permission.Name;
                }

                var model = new OperateLogModel
                {
                    Area = area,
                    Controller = controller,
                    Action = action,
                    Description = description,
                    LogTime = DateTime.Now,
                    UserId = user.Id,
                    LoginName = user.LoginName,
                    IPAddress = Tools.GetUserIp()
                };

                OperateLogService.Insert(model);
            }
        }
    }
}